package org.xqyy.eval.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.xqyy.eval.model.TDept;
import org.xqyy.eval.model.TQuestion;
import org.xqyy.eval.model.TUser;
import org.xqyy.eval.model.TestPaper;
import org.xqyy.eval.service.IDeptMapperService;
import org.xqyy.eval.service.IQuestionService;
import org.xqyy.eval.service.ITSuggestionService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/deptMapper")
public class DeptMapperController {

    @Autowired
    private IDeptMapperService deptMapperService;
    @Autowired
    private IQuestionService questionService;
    @Autowired
    private ITSuggestionService suggestionService;


    /**
     * 根据科室ID查询该科室可以评价的已经绑定了试卷的科室
     * @return
     */
    @RequestMapping("/selectEvalDeptByDeptId")
    @ResponseBody
    public  List<TDept> selectEvalDeptByDeptId(String deptId){
        TDept dept = new TDept();
        dept.setId(deptId);
        return  deptMapperService.findEvalDeptListByDeptid(dept);
    }

    /**
     * 添加科室评价科室关系
     * @param tDept 科室
     * @param evalDeptIdList 可以评价科室id
     * @return
     */
    @RequestMapping("/addDeptMapper")
    @ResponseBody
    public  Boolean addDeptMapper(TDept tDept,
                                  @RequestParam(required = false, value = "evalDeptIdList[]") List<String> evalDeptIdList ){
        try {
            List<TDept> evalDeptArrayList = new ArrayList<>();
            for (String evalDeptId : evalDeptIdList) {
                TDept evalDept = new TDept();
                evalDept.setId(evalDeptId);
                evalDeptArrayList.add(evalDept);
            }
            deptMapperService.updateDeptMapper(tDept,evalDeptArrayList);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }


    /**
     * 拷贝科室评价科室关系
     * @param tDept 参考科室
     * @param copyToDeptIdList 拷贝到科室id
     * @return
     */
    @RequestMapping("/copyDeptMapper")
    @ResponseBody
    public  Boolean copyDeptMapper(TDept tDept,
                                  @RequestParam(required = false, value = "copyToDeptIdList[]") List<String> copyToDeptIdList ){
        try {
            List<TDept> copyToDeptList = new ArrayList<>();
            for (String copyToDeptId : copyToDeptIdList) {
                TDept copyToDuty = new TDept();
                copyToDuty.setId(copyToDeptId);
                copyToDeptList.add(copyToDuty);
            }
            deptMapperService.copyDeptMapper(tDept,copyToDeptList);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 根据科室id查询该科室应当评价的科室及其关联的
     * 调查问卷和问题
     * @param tDept 要查询的科室信息
     * @return
     */
    @RequestMapping("/selectEvalDeptPaperByDeptId")
    @ResponseBody
    public List<TDept> selectEvalDeptPaperByDeptId(TDept tDept){
        return deptMapperService.selectEvalDeptPaperByDeptId(tDept);
    }



    /**
     * 根据科室ID查询该科室可以评价的科室
     * @return
     */
    @RequestMapping("/findEvalDeptList")
    @ResponseBody
    public  List<String> findEvalDeptList(TDept tDept){
        return  deptMapperService.findEvalDeptList(tDept);
    }
    @RequestMapping("/addDeptPaperForm")
    public  ModelAndView addDeptPaperForm(String deptid){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("jsp/addDeptPaperForm");
        modelAndView.addObject("deptid",deptid);
        return modelAndView;
    }

    @RequestMapping("/addDeptPaper")
    @ResponseBody
    public  boolean addDeptPaper(String deptid,
                                 @RequestParam(required = false, value = "evaldutyIdList[]") List<String> evaldutyIdList,
                                 Integer paperId){
        TDept dept = new TDept();
        dept.setId(deptid);
        List<TDept> evalDeptList = new ArrayList<TDept>();
        for (String evalDeptId : evaldutyIdList) {
            TDept evaldept = new TDept();
            evaldept.setId(evalDeptId);
            evalDeptList.add(evaldept);
        }
        deptMapperService.updateDeptMapperPaper(dept,evalDeptList,paperId);
        return true;
    }


    @RequestMapping("/selectDeptUpcomingPaperPage")
    public  ModelAndView selectDeptUpcomingPaperPage(HttpServletRequest request){
        HttpSession session = request.getSession();
        TUser user = (TUser)  session.getAttribute("user");
        Boolean isAllow = suggestionService.isAllowSubmitSuggestion(user);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("jsp/eval/evalmainpage");
        modelAndView.addObject("deptid",user.getDeptId());
        modelAndView.addObject("isAllow",isAllow);
        return modelAndView;
    }

    @RequestMapping("/selectDeptUpcomingPaper")
    @ResponseBody
    public  List<TestPaper> selectDeptUpcomingPaper(String deptid){
        return deptMapperService.selectDeptUpcomingPaper(deptid);
    }


    /**
     * 生成评价试卷
     * @param request
     * @param testPaper
     * @return
     */
    @RequestMapping("/findEvalDeptListByDeptId")
    @ResponseBody
    public  List<Map<String,Object>> findEvalDeptListByDeptId(HttpServletRequest request, TestPaper testPaper){
        List<Map<String,Object>> resultList = new ArrayList<>();
        HttpSession session = request.getSession();
        TUser user = (TUser)  session.getAttribute("user");
        List<TDept> deptList =  deptMapperService.findEvalDeptListByDeptId(user.getDeptId(),testPaper.getPaperId());
        List<TQuestion> questionList = questionService.selectQuestionByPaperId(testPaper.getPaperId());
        for (TDept dept : deptList) {
            Map<String,Object> map = new HashMap<>();
            map.put("id",dept.getId());
            map.put("name",dept.getName());
            map.put("context","如不满意需填写说明");
            for (TQuestion tQuestion : questionList) {
                String key = "question"+tQuestion.getId();
                map.put(key,20);
            }
            resultList.add(map);
        }
        return  resultList;
    }
}
